package cn.ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ssm.biz.impl.StudentBizimpl;
import cn.ssm.biz.impl.UserBizImpl;
import cn.ssm.pojo.Pager;
import cn.ssm.pojo.Student;
import cn.ssm.pojo.User;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	@Qualifier("UBizImpl")
	private UserBizImpl UserBizimpl;
	
	@Autowired
	@Qualifier("studentBiz")
	private StudentBizimpl studentBizimpl;
	

	public StudentBizimpl getStudentBizimpl() {
		return studentBizimpl;
	}

	
	
	public void setStudentBizimpl(StudentBizimpl studentBizimpl) {
		this.studentBizimpl = studentBizimpl;
	}

	public UserBizImpl getUserBizimpl() {
		return UserBizimpl;
	}

	public void setUserBizimpl(UserBizImpl userBizimpl) {
		UserBizimpl = userBizimpl;
	}

	@ResponseBody
	@RequestMapping("/login")
	public String userlogin(@RequestParam("userName") String userName,
			@RequestParam("password") String password, ModelMap mo,HttpSession session) {

		User u = new User();
		u.setUserName(userName);
		u.setUserPwd(password);
		System.out.println("ss:" + userName + password);
		String url = null;
		
		User ur = UserBizimpl.findlogin(u);
		if (ur != null) {
			String ro = ur.getRoName();
			mo.put("user", u);// 用户名
			mo.put("ur", ur);// 权限
			int userId=ur.getId();
			System.out.println();
			List<Student> stuList= studentBizimpl.getStuByUserName(userName);
			
			Student student = null;
			if(stuList != null && stuList.size()>0){
				student = stuList.get(0);
			}
			mo.put("stuList", stuList);
			System.out.println("=================stuList======="+stuList.size());
			System.out.println("进入login--身份是" + ro);
			System.out.println(ur+"====================");
			session.setAttribute("user", ur);
			
			if (ro.equals("组长")) {
				url = String.valueOf(student.getConputerroomId());
//				url="CR_One";

			} else if (ro.equals("老师")) {
				url = "index2";

			} else if (ro == "维修人员" || ro.equals("维修人员")) {

				url = "index4";
			} else if (ro == "管理员" || ro.equals("管理员")) {
				
				mo.put("ro", ro);
				System.out.println("ur"+ur.getRoName());
				url = "index";
			}else if (ro == "普通学生" || ro.equals("普通学生")) {

				url = "index3";
			}
			
			session.setAttribute("user", ur);
			
			
		} else {
			url = "login";

		}
		return url;

	}
	
	@RequestMapping("/findUsers")
	public String findUsersAll(ModelMap modelMap,HttpServletRequest request){
		String currentPageStr =request.getParameter("currentPage");
		//System.out.println(currentPageStr);
		int rowCount = UserBizimpl.findUserCount();
		Pager pager=null;
		if(currentPageStr==null){
			pager=new Pager(rowCount, 5, 1);
		}else{
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			pager=new Pager(rowCount, 5, currentPage);
		}
		pager.setUrl("/user/findUsers");
		List<User> userList =UserBizimpl.findUserList(pager);
		modelMap.put("pager", pager);
		String url = "";
		if(userList != null){
			modelMap.put("userList", userList);
			url = "navbar";
		}else{
			url = "404";
			
		}
		return url;
	}
	
	

	@RequestMapping("/addUaers")
	public String addUsersAll(User u,HttpSession session,int roleId){
		String url = "";
		int a = UserBizimpl.addUsers(u);
		session.setAttribute("roleId", roleId);
		if(a > 0){
			url = "redirect:/userrole/addUserRole";
		}else{
			url="404";
		}
		return url;
	}
	
	@RequestMapping("/delUsers")
	public String delUsersAll(int id){
		int num = UserBizimpl.delUsers(id);
		String url = "";
		if(num > 0){
			url = "navbar";
		}else{
			url="404";
		}
		return url;
	}
	
	@RequestMapping("/updateUsers")
	public String updateUsersAll(User u,HttpSession session,int roleId){
		String url = "";
		int uidxx=u.getId();
		session.setAttribute("uidxx", uidxx);
		session.setAttribute("upRoleId", roleId);
		int num = UserBizimpl.UpdateUsers(u);
		if(num > 0){
			url = "redirect:/userrole/updateUserRole";
		}else{
			url="404";
		}
		return url;
	}
	@RequestMapping("/findUsersByNo")
	public String FindUsersByNo(int id,HttpSession session){
		session.setAttribute("fuid", id);
		User u = UserBizimpl.findUserslistByid(id);
		String url = "";
		if(u != null){
			session.setAttribute("u", u);
			url = "redirect:/userrole/findUserRoleByNo";
		}else{
			url = "404";
		}
		return url;
	}
//	@RequestMapping("/laout")
//	public String laout(HttpSession session){
//		
//		String url="";
//		User user=(User)session.getAttribute("user");
//		if (user!=null){
//			session.invalidate();
//			url="login";
//			System.out.println("user"+user.getId());
//		}
//		
//		
//		return url;
//		
//		
//	}
}


